import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class MyRenderWidget extends StatefulWidget {
  @override
  _MyRenderWidgetState createState() => _MyRenderWidgetState();
}

class _MyRenderWidgetState extends State<MyRenderWidget> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('自定义RenderWidget')),
      body: Container(
        color: Colors.purple[200],
        child: Column(
          children: [
            ShadowBox(
                child: FlutterLogo(
              size: 100,
            )),
            ShadowBox(
                child: Icon(Icons.category, size: 100),
                distance: 10,
                key: UniqueKey()),
            ShadowBox(
                child: Icon(Icons.category_outlined, size: 100),
                distance: 10),
          ],
        ),
      ),
    );
  }
}

class ShadowBox extends SingleChildRenderObjectWidget {
  // 直接传递的时候 可能没有变化 此时 两种解决方案 1 加key 2updateRenderObject 修改比较
  final double distance;
  final Key key;
  ShadowBox({Widget child, this.distance, this.key}) : super(child: child);
  @override
  RenderObject createRenderObject(BuildContext context) {
    return RenderShadowBox(distance);
  }

  @override
  void updateRenderObject(
      BuildContext context, covariant RenderShadowBox renderObject) {
    // super.updateRenderObject(context, renderObject);
    renderObject.distance = distance;
  }
}

// class RenderShadowBox extends RenderBox with RenderObjectWithChildMixin {
class RenderShadowBox extends RenderProxyBox with DebugOverflowIndicatorMixin {
  // DebugOverflowIndicatorMixin    overflow的罪魁祸首
  double distance;

  RenderShadowBox(this.distance);
  @override
  void performLayout() {
    //1 如下可以设置当前 以及child的大小  此时box本身和child无关 若相关联 往下看
    // child.layout(constraints);
    // child.layout(BoxConstraints.loose(Size(150,150)));
    // size = Size(200, 300);
    //2 此时box 和 child大小关联
    // 首先声明  然后使用
    // 默认不关联的 好处就是优化布局
    // relayout Boundry
    child.layout(constraints, parentUsesSize: true);
    size = (child as RenderProxyBox).size;

    // super.performLayout();
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    print(size);
    context.paintChild(child, offset);
    context.pushOpacity(offset, 127, (context, offset) {
      // print(offset);
      context.paintChild(child, Offset(distance, distance));
    });

    // 添加别的东西
    // context.canvas.drawColor(Colors.red, BlendMode.screen);
    // context.canvas.drawLine(
    //     offset * 1.2,
    //     offset * 1.5,
    //     Paint()
    //       ..color = Colors.green // 画笔的颜色
    //       ..strokeWidth = 20.0 // 线的宽度
    //       ..style = PaintingStyle.stroke // fill: 填充， stroke: 空心
    //       ..strokeCap = StrokeCap.butt // 转折处的处理
    //       ..blendMode =
    //           BlendMode.color // 重叠处的处理模式，clear,src, dst, srcIn,dstIn等等。
    //     );
    // super.paint(context, offset);
    paintOverflowIndicator(
        context,
        offset,
        Offset.zero & size*2,
        // Rect.fromLTWH(0, 0, size.width, size.height),
        Rect.fromLTWH(0, 0, 200, 200));
  }
}
